from django.core.validators import FileExtensionValidator
from rest_framework import serializers


class UploadImageSerializers(serializers.Serializer):
    image = serializers.ImageField(
        validators=[FileExtensionValidator(['png', 'jpg', 'jpeg', 'dif'])],
        error_messages={'request': '请上传图片', 'invalid_image': '请上传符合格式的图片'}
    )

    def validate_image(self, value):
        max_size = 1024 * 1024 * 2
        if value.size > max_size:
            raise serializers.ValidationError('图片大小不能超过2M')
        return value
